unit unitSaveSettings;

interface

uses Windows, IniFiles, SysUtils, Dialogs;

procedure LoadVisualsFromSettings;
procedure LoadSettingsFromIniFiles;
procedure SaveVisualsToIni;

implementation

uses Unit1, Unit2;

var
  BAddress: String;
  BPort: String;

procedure LoadVisualsFromSettings;
var
  Ini: TIniFile;
begin
  Form1.ExtVisualization.Checked := Form1.bExtThreadsVisualization;
  Form1.LogToFile.Checked := Form1.bExtLogFile;
  Form1.useDataCompression.Checked := Form1.bUseEvilCompression;

  Form1.ThreadsShowTime.Enabled := Form1.ExtVisualization.Checked;
  Form1.CPUUsageLimit.Enabled := Form1.useDataCompression.Checked;
  Form1.ThreadsShowTime.Text := IntToStr(Form1.ShowTime);

  Ini := TIniFile.Create(Form1.EXEPath + 'servers.ini');

  Form1.ExternalPort.Text := IntToStr(Ini.ReadInteger('server0', 'Port', 660));
  Form1.BackyardIP.Text := Ini.ReadString('server0', 'BackyardAddress',
    '127.0.0.1');
  Form1.Timeout.Text := IntToStr(Form1.dwTimeOut);
  Form1.CPUUsageLimit.Text := IntToStr(Form1.iMaxCPUUsage);

  Form1.Shutdown.Checked := Form1.bGraceShutdown;

  Form1.IsLimitThreadCount.Checked := Form1.bIsThreadsCountLimit;
  Form1.ThreadsCountLimit.Text := IntToStr(Form1.iThreadsCountLimit);
  Form1.TotalCPUUsageLimit.Text := IntToStr(Form1.iMaxTotalCPUUsage);

  Form1.ThreadsCountLimit.Enabled := Form1.bIsThreadsCountLimit;

  Ini.Free;
end;

procedure LoadSettingsFromIniFiles;
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create(Form1.EXEPath + 'settings.ini');
  Form1.bExtThreadsVisualization :=
    (Ini.ReadString('visuals', 'extthreads', 'false') = 'true');
  Form1.bExtLogFile := (Ini.ReadString('visuals', 'extlogfile', 'false')
    = 'true');
  Form1.ShowTime := Ini.ReadInteger('visuals', 'showtime', 1000);
  Form1.bGraceShutdown := (Ini.ReadString('visuals', 'graceshutdown', 'false')
    = 'true');
  Ini.Free;

  Ini := TIniFile.Create(Form1.EXEPath + 'servers.ini');
  Form1.bUseEvilCompression :=
    (Ini.ReadString('server0', 'compression', 'false') = 'true');
  Form1.bUseEvilCompression_CPU := Form1.bUseEvilCompression;
  Form1.iMaxCPUUsage := Ini.ReadInteger('server0', 'maxcpuusage', 100);
  Form1.dwTimeOut := Ini.ReadInteger('server0', 'timeout', 1000);

  Form1.bIsThreadsCountLimit :=
    (Ini.ReadString('server0', 'isthrlimit', 'false') = 'true');
  Form1.iThreadsCountLimit := Ini.ReadInteger('server0', 'thrlimit', 25);

  Form1.iMaxTotalCPUUsage := Ini.ReadInteger('server0', 'totalcpulimit', 100);

  Ini.Free;
end;

procedure SaveVisualsToIni;
var
  Ini: TIniFile;
  {
    Changes: BOOL;
  }
begin
  {
    Changes := (BAddress <> '') and (BPort <> '') and
    ((Form1.BackyardIP.Text <> BAddress) or (Form1.ExternalPort.Text <> BPort));
  }

  //mustadd: check for integers type!!
  if StrToInt(Form1.ThreadsShowTime.Text) < 1000 then
    Form1.ThreadsShowTime.Text := '1000';

  if (StrToInt(Form1.CPUUsageLimit.Text) > 100) or
    (StrToInt(Form1.CPUUsageLimit.Text) < 0) then
    Form2.Edit5.Text := '100';

  if (StrToInt(Form1.TotalCPUUsageLimit.Text) > 100) or
    (StrToInt(Form1.TotalCPUUsageLimit.Text) < 0) then
    Form2.Edit5.Text := '100';

  Ini := TIniFile.Create(Form1.EXEPath + 'settings.ini');

  Ini.WriteInteger('visuals', 'showtime', StrToInt(Form1.ThreadsShowTime.Text));

  if Form1.ExtVisualization.Checked then
    Ini.WriteString('visuals', 'extthreads', 'true')
  else
    Ini.WriteString('visuals', 'extthreads', 'false');

  if Form1.LogToFile.Checked then
    Ini.WriteString('visuals', 'extlogfile', 'true')
  else
    Ini.WriteString('visuals', 'extlogfile', 'false');

  if Form1.Shutdown.Checked then
    Ini.WriteString('visuals', 'graceshutdown', 'true')
  else
    Ini.WriteString('visuals', 'graceshutdown', 'false');
  Ini.Free;

  Ini := TIniFile.Create(Form1.EXEPath + 'servers.ini');
  Ini.WriteString('server0', 'BackyardAddress', Form1.BackyardIP.Text);
  Ini.WriteInteger('server0', 'Port', StrToInt(Form1.ExternalPort.Text));

  if Form1.useDataCompression.Checked then
    Ini.WriteString('server0', 'compression', 'true')
  else
    Ini.WriteString('server0', 'compression', 'false');

  Ini.WriteInteger('server0', 'maxcpuusage',
    StrToInt(Form1.CPUUsageLimit.Text));

  if Form1.IsLimitThreadCount.Checked then
    Ini.WriteString('server0', 'isthrlimit', 'true')
  else
    Ini.WriteString('server0', 'isthrlimit', 'false');

  Ini.WriteInteger('server0', 'thrlimit',
    StrToInt(Form1.ThreadsCountLimit.Text));

  Ini.WriteInteger('server0', 'totalcpulimit',
    StrToInt(Form1.TotalCPUUsageLimit.Text));

  Ini.WriteInteger('server0', 'timeout', StrToInt(Form1.Timeout.Text));
  Ini.Free;

  BAddress := Form1.BackyardIP.Text;
  BPort := Form1.ExternalPort.Text;

  {
    if Changes then
    ShowMessage('To apply this configuration you must restart program');
  }
end;

end.
